<?php
$authenticationService = new AuthenticationService();
$host = Tool::decode($smtp['host'], "sTunnInVietnAm@2012#");
$port = Tool::decode($smtp['port'], "sTunnInVietnAm@2012#");
$user = Tool::decode($smtp['user'], "sTunnInVietnAm@2012#");
$pass = Tool::decode($smtp['pass'], "sTunnInVietnAm@2012#");
$emailService = new EmailService($host, $port, $user, $pass);

$accountService = new AccountService();

switch ($action) {
	case "editpermit":
		if($permit == true)
		{
			$checkArr = $_POST['check'];
			$moduleID = $_POST['moduleID'];
			$permitService = new PermitService();
			$data = array();
			$permitDomain = new PermitDomain($data);
			$permitDomain->setID(null);
			$permitDomain->setModuleID($moduleID);
			$permitDomain->setActive(0);
			//uncheck all
			$permitDomain->setUserType(1);
			$permitService->resetPermit($context, $result, $permitDomain);
				
			$permitDomain->setUserType(2);
			$permitService->resetPermit($context, $result, $permitDomain);
				
			$permitDomain->setUserType(3);
			$permitService->resetPermit($context, $result, $permitDomain);
				
			$permitDomain->setUserType(4);
			$permitService->resetPermit($context, $result, $permitDomain);
				
			foreach($checkArr as $check)
			{
				if($check == 1)
				{
					$permitDomain->setUserType(3);
					$permitDomain->setActive(1);
					$permitDomain->setID($permitService->getPermitIDByModuleIDUserType($context, $result, $moduleID, 3));
					$permitService->updatePermit($context, $result, $permitDomain);
				}
				if($check == 2)
				{
					$permitDomain->setUserType(4);
					$permitDomain->setActive(1);
					$permitDomain->setID($permitService->getPermitIDByModuleIDUserType($context, $result, $moduleID, 4));
					$permitService->updatePermit($context, $result, $permitDomain);
				}
				if($check == 3)
				{
					$permitDomain->setUserType(1);
					$permitDomain->setActive(1);
					$permitDomain->setID($permitService->getPermitIDByModuleIDUserType($context, $result, $moduleID, 1));
					$permitService->updatePermit($context, $result, $permitDomain);
				}
				if($check == 4)
				{
					$permitDomain->setUserType(2);
					$permitDomain->setActive(1);
					$permitDomain->setID($permitService->getPermitIDByModuleIDUserType($context, $result, $moduleID, 2));
					$permitService->updatePermit($context, $result, $permitDomain);
				}
			}
			header("Location:index.php?func=web&opt=permit");
		}
		else
		header("Location:index.php?func=dash");
		break;

	case "editEmail":
		if($permit == true)
		{
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = addslashes($value);
			}
			$emailDomain = new EmailDomain($data);
				
			$emailService->updateEmailByObj($context, $result, $emailDomain);
			header("Location:index.php?func=web&opt=email");
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "addEmail":
		if($permit == true)
		{
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = addslashes($value);
			}
			$emailDomain = new EmailDomain($data);
			$emailService->createEmail($context, $result, $emailDomain);
			header("Location:index.php?func=web&opt=email");
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "deleteEmail":
		if($permit == true)
		{
			$mailID = $_GET['id'];
			$emailService->deleteEmailByOid($context, $result, $mailID);
			header("Location:index.php?func=web&opt=email");
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "updateCompany":
		if($permit == true)
		{
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = addslashes($value);
			}
			$account = $accountService->getAccountByOid($context, $result, 1);

			$resultMerge = array_merge($account->getAccountDomain()->toArray(), $data);
			$account->setAccountDomain(new AccountDomain($resultMerge));

				
			$resultMerge = array_merge($account->getBankDomain()->toArray(), $data);
			$account->setBankDomain(new BankDomain($resultMerge));
				
			$resultMerge = array_merge($account->getCompanyDomain()->toArray(), $data);
			$account->setCompanyDomain(new CompanyDomain($resultMerge));
				
			$accountService->updateAccount($context, $result, $account);
			header("Location:index.php?func=web&opt=companyInfo");
		}
		else
			header("Location:index.php?func=dash");
		break;
			
	case "readTerms":
		if($permit == true)
		{
			if(isset($_POST['read']))
			{
				$userService = new UserService();
				$userService->updateStatus($context, $result, $_POST['userID'], 1);
				header("Location:index.php?func=dash");
			}
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	default:
		echo "khong co tinh nang nay";
		break;
}
?>